package com.hiultra.archive.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hiultra.archive.domain.ArchiveStorage;
import com.hiultra.archive.domain.ArchiveStorageBox;
import com.hiultra.archive.domain.ArchiveStorageTask;
import com.hiultra.archive.service.IArchiveStorageBoxService;
import com.hiultra.archive.service.IArchiveStorageService;
import com.hiultra.archive.service.IArchiveStorageTaskService;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.page.TableDataInfo;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URISyntaxException;
import java.util.List;

/**
 * 档案入库任务Controller
 *
 * @author ruoyi
 * @date 2024-07-22
 */
@RestController
@RequestMapping("/archive/storageTask")
public class ArchiveStorageTaskController extends BaseController {
    @Autowired
    private IArchiveStorageTaskService archiveStorageTaskService;
    @Autowired
    private IArchiveStorageBoxService archiveStorageBoxService;
    @Autowired
    private IArchiveStorageService archiveStorageService;

    /**
     * 查询档案入库任务列表
     */
    @PreAuthorize("@ss.hasPermi('archive:storageTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArchiveStorageTask archiveStorageTask) {
        startPage();
        List<ArchiveStorageTask> list = archiveStorageTaskService.selectArchiveStorageTaskList(archiveStorageTask);
        return getDataTable(list);
    }

    /**
     * 导出档案入库任务列表
     */
    @PreAuthorize("@ss.hasPermi('archive:storageTask:export')")
    @Log(title = "档案入库任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArchiveStorageTask archiveStorageTask) throws JsonProcessingException {
        List<ArchiveStorageTask> list = archiveStorageTaskService.selectArchiveStorageTaskList(archiveStorageTask);
        for (ArchiveStorageTask archiveStorageTask1 : list) {
            List<ArchiveStorageBox> archiveStorageBoxList = archiveStorageBoxService.selectArchiveStorageBoxList(new ArchiveStorageBox() {{
                setTaskId(archiveStorageTask1.getTaskId());
            }});

            for (ArchiveStorageBox archiveStorageBox:archiveStorageBoxList) {
                List<ArchiveStorage> archiveStorageList = archiveStorageService.selectArchiveStorageList(new ArchiveStorage() {{
                    setTaskBoxId(archiveStorageBox.getId());
                }});
                archiveStorageBox.setArchiveStorageList(archiveStorageList);
            }

            archiveStorageTask1.setArchiveStorageBoxList(archiveStorageBoxList);
        }
        ExcelUtil<ArchiveStorageTask> util = new ExcelUtil<ArchiveStorageTask>(ArchiveStorageTask.class);
        util.exportExcel(response, list, "档案入库任务数据");
    }

    /**
     * 获取档案入库任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('archive:storageTask:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") String taskId) {
        return AjaxResult.success(archiveStorageTaskService.selectArchiveStorageTaskByTaskId(taskId));
    }

    /**
     * 获取档案入库任务详细档案信息
     */
    @PreAuthorize("@ss.hasPermi('archive:storageTask:query')")
    @GetMapping(value = "selectArchiveStorageByTaskId/{taskId}")
    public AjaxResult selectArchiveStorageByTaskId(@PathVariable("taskId") String taskId) {
        return AjaxResult.success(archiveStorageService.selectArchiveStorageByTaskId(taskId));
    }
    /**
     * 新增档案入库任务
     */
    @PreAuthorize("@ss.hasPermi('archive:storageTask:add')")
    @Log(title = "档案入库任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArchiveStorageTask archiveStorageTask) {
        return toAjax(archiveStorageTaskService.insertArchiveStorageTask(archiveStorageTask));
    }

    /**
     * 修改档案入库任务
     */
    @PreAuthorize("@ss.hasPermi('archive:storageTask:edit')")
    @Log(title = "档案入库任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArchiveStorageTask archiveStorageTask) {
        return toAjax(archiveStorageTaskService.updateArchiveStorageTask(archiveStorageTask));
    }

    /**
     * 删除档案入库任务
     */
    @PreAuthorize("@ss.hasPermi('archive:storageTask:remove')")
    @Log(title = "档案入库任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable String[] taskIds) {
        return toAjax(archiveStorageTaskService.deleteArchiveStorageTaskByTaskIds(taskIds));
    }
}
